package com.dk.config;

import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.alibaba.druid.filter.logging.Slf4jLogFilter;
import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInterceptor;

@Configuration
@EnableTransactionManagement
@MapperScan("com.dk.mapping")
public class DAO implements EnvironmentAware {
	public static DataSource dataSource;

	private RelaxedPropertyResolver propertyResolver;

	@Bean(destroyMethod = "close")
	@Primary
	public DataSource dataSource() throws SQLException {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName("org.sqlite.JDBC");
		dataSource.setUrl(propertyResolver.getProperty("url"));
//		dataSource.setFilters("mergeStat");
//		dataSource.getProxyFilters().add(slf4jLogFilter());
		DAO.dataSource = dataSource;
		return dataSource;
	}
	
	public Slf4jLogFilter slf4jLogFilter() {
		Slf4jLogFilter logFilter = new Slf4jLogFilter();
		logFilter.setConnectionLogEnabled(true);
		logFilter.setStatementLogEnabled(true);
		logFilter.setResultSetLogEnabled(true);
		logFilter.setStatementExecutableSqlLogEnable(true);
		return logFilter;
	}

	@Bean
	public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
		SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
		sessionFactory.setDataSource(dataSource);
		sessionFactory.setTypeAliasesPackage("com.dk.entity");
//		PageInterceptor pageHelper=new PageInterceptor();
//		Properties properties=new Properties();
//		properties.setProperty("helperDialect", "mysql");
//		pageHelper.setProperties(properties);
//		Interceptor[] item=new Interceptor[]{pageHelper};
//		sessionFactory.setPlugins(item);
		return sessionFactory.getObject();
	}
	@Bean
	public DbSearcher dbSearcher(DataSource dataSource) throws Exception {
		DbSearcher _searcher = new DbSearcher(new DbConfig(), propertyResolver.getProperty("ip"));
		return _searcher;
	}

	@Override
	public void setEnvironment(Environment environment) {
		this.propertyResolver = new RelaxedPropertyResolver(environment, "jdbc.");
	}

}